package com.guigu.limeng.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.guigu.aop.OpLog;
import com.guigu.limeng.pojo.Mdesignprocedure;
import com.guigu.limeng.pojo.Mdesignproceduredetails;
import com.guigu.limeng.pojo.Mdesignproceduremoduledetails;
import com.guigu.limeng.service.MdesignprocedureService;
import com.guigu.limeng.service.MdesignproceduredetailsService;
import com.guigu.limeng.service.MdesignproceduremoduledetailsService;
import com.guigu.liuzhireng.pojo.PageVo;
import com.guigu.tankai.pojo.Dfile;
import com.guigu.tankai.pojo.Dmoduledetails;
import com.guigu.tankai.service.DfileService;
import com.guigu.tankai.service.DmoduledetailsService;
import com.guigu.util.service.IDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("Mdesignprocedure.do")
public class MdesignprocedureController {
    @Autowired
    MdesignprocedureService mdesignprocedureService;
    @Autowired
    IDService idService;
    @Autowired
    DfileService dfileService;
    @Autowired
    MdesignproceduremoduledetailsService mdesignproceduremoduledetailsService;
    @Autowired
    MdesignproceduredetailsService mdesignproceduredetailsService;
    @Autowired
    DmoduledetailsService dmoduledetailsService;

    @RequestMapping("/AddMdesignprocedure")
    @ResponseBody
    @OpLog(opModule = "生产管理模块",operType = "添加生产工序设计",operDesc = "此方法用于制定产品生产工序设计单")
    public Mdesignprocedure AddMdesignprocedure(Mdesignprocedure mdesignprocedure) {
        mdesignprocedure.setDesignid(idService.PDS_ID());
        dfileService.upById(mdesignprocedure.getProductid());
        return mdesignprocedureService.AddMdesignprocedure(mdesignprocedure);
    }

    @RequestMapping("/showMdeure")
    @ResponseBody
    public PageVo<Mdesignprocedure> showMdeure(@RequestBody PageVo<Mdesignprocedure> pageVo) {
        return mdesignprocedureService.showMdeure(pageVo.getNum(), pageVo.getSize());
    }

    @RequestMapping("/upBySHBZ")
    @OpLog(opModule = "生产管理模块",operType = "制定产品生产工序设计单审核",operDesc = "此方法用于制定产品生产工序设计单审核")
    public String upBySHBZ(String mdList, String dfID) {
        Mdesignprocedure mdesignprocedure = new Gson().fromJson(mdList, Mdesignprocedure.class);
        mdesignprocedure.setDesignmoduletag("G002-0");
        mdesignprocedureService.upBySHBZ(mdesignprocedure);
        if (mdesignprocedure.getChecktag().equals("S001-2")) {
            Dfile dfile = new Dfile();
            dfile.setProductid(dfID);
            dfile.setDesignproceduretag("G001-1");
            dfileService.DfileUpDate_ZT_LM(dfile);
        }
        return "/li_meng/CPSCGXShengHe.html";
    }

    @RequestMapping("/PageChaXun")
    @ResponseBody
    public PageVo<Mdesignprocedure> PageChaXun(@RequestBody PageVo<Mdesignprocedure> pageVo) {
        return mdesignprocedureService.PageChaXun(pageVo.getNum(), pageVo.getSize());
    }

    @RequestMapping("/PageBianGeng")
    @ResponseBody
    public PageVo<Mdesignprocedure> pageInfo(@RequestBody PageVo<Mdesignprocedure> pageVo) {
        return mdesignprocedureService.PageBianGeng(pageVo.getNum(), pageVo.getSize());
    }

    @RequestMapping("/findWuLiao")
    @ResponseBody
    public PageVo<Mdesignprocedure> findWuLiao(@RequestBody PageVo<Mdesignprocedure> pageVo) {
        return mdesignprocedureService.findWuLiao(pageVo.getNum(), pageVo.getSize());
    }

    @RequestMapping("/updaByWuLiao")
    @ResponseBody
    public int updaByWuLiao(String productid) {
        return mdesignprocedureService.updaByWuLiao(productid);
    }

    @RequestMapping("/LiMengMdesignprocedure")
    @OpLog(opModule = "生产管理模块",operType = "添加工序物料设计",operDesc = "此方法用于制定工序物料设计单")
    public String LiMengMdesignprocedure(String MDesignProcedureDetails, String shu, String DModuleDetails) {
        Mdesignproceduredetails mapplies = new Gson().fromJson(MDesignProcedureDetails, Mdesignproceduredetails.class);
        List<Integer> ids = new Gson().fromJson(shu, new TypeToken<List<Integer>>() {
        }.getType());
        List<Dmoduledetails> dmoduledetails = new Gson().fromJson(DModuleDetails, new TypeToken<List<Dmoduledetails>>() {
        }.getType());

        // 产品生产工序物料明细 Del
        mdesignproceduremoduledetailsService.limeng_Del_Mdesignproceduremoduledetails(mapplies.getId());
        int wuliaoXJ = 0;
        // 产品生产工序物料明细 Add
        for (int i = 0; i < dmoduledetails.size(); i++) {
            Mdesignproceduremoduledetails mdesignproceduremoduledetails = new Mdesignproceduremoduledetails();
            mdesignproceduremoduledetails.setParentid(mapplies.getId());
            mdesignproceduremoduledetails.setDetailsnumber(dmoduledetails.get(i).getDetailsnumber());
            mdesignproceduremoduledetails.setProductid(dmoduledetails.get(i).getProductid());
            mdesignproceduremoduledetails.setProductname(dmoduledetails.get(i).getProductname());
            mdesignproceduremoduledetails.setType(dmoduledetails.get(i).getType());
            mdesignproceduremoduledetails.setAmount(ids.get(i));

            // 产品物料组成明细 update 可用数量
            Dmoduledetails dm = new Dmoduledetails();
            dm.setId(dmoduledetails.get(i).getId());
            dm.setResidualamount(dmoduledetails.get(i).getResidualamount()-ids.get(i));
            dmoduledetailsService.limeng_UpDate_Dmoduledetails(dm);

            mdesignproceduremoduledetails.setProductdescribe(dmoduledetails.get(i).getProductdescribe());
            mdesignproceduremoduledetails.setAmountunit(dmoduledetails.get(i).getAmountunit());
            mdesignproceduremoduledetails.setCostprice(dmoduledetails.get(i).getCostprice());
            mdesignproceduremoduledetails.setSubtotal(dmoduledetails.get(i).getCostprice() * ids.get(i));

            mdesignproceduremoduledetailsService.limeng_Add_Mdesignproceduremoduledetails(mdesignproceduremoduledetails);
            wuliaoXJ += dmoduledetails.get(i).getCostprice() * ids.get(i);
        }

        // 产品生产工序明细 update
        Mdesignproceduredetails mdesignproceduredetails = new Mdesignproceduredetails();
        mdesignproceduredetails.setId(mapplies.getId());
        mdesignproceduredetails.setModulesubtotal(wuliaoXJ);
        mdesignproceduredetails.setDesignmoduletag("D002-1");
        mdesignproceduredetailsService.updateByMingXi(mdesignproceduredetails);
        return "/li_meng/GXWLSheJiDan.html";
    }

    @RequestMapping("/LiMengMdesignprocedureUpdate")
    public String LiMengMdesignprocedureUpdate(Mdesignprocedure Mdesignprocedure) {
        mdesignprocedureService.limengUpdate(Mdesignprocedure);
        List<Dmoduledetails> list = dmoduledetailsService.ChaWuLiao(Mdesignprocedure.getProductid());
        for (Dmoduledetails dm : list) {
            Dmoduledetails dmoduledetails = new Dmoduledetails();
            dmoduledetails.setId(dm.getId());
            dmoduledetails.setResidualamount(dm.getAmount());
            dmoduledetailsService.limeng_UpDate_Dmoduledetails(dmoduledetails);
        }
        return "/li_meng/GXWLSheJiDan.html";
    }

    @RequestMapping("/LiMengMdesignprocedureSH")
    @ResponseBody
    public PageVo<Mdesignprocedure> LiMengMdesignprocedureSH(@RequestBody PageVo<Mdesignprocedure> pageVo) {
        return mdesignprocedureService.LiMengMdesignprocedureSH(pageVo.getNum(), pageVo.getSize());
    }

    @RequestMapping("/LiMengMdesignprocedureSHZT")
    @OpLog(opModule = "生产管理模块",operType = "工序物料设计单审核",operDesc = "此方法用于工序物料设计单审核")
    public String LiMengMdesignprocedureSHZT(String mdList, String dfID) {
        Mdesignprocedure mdesignprocedure = new Gson().fromJson(mdList, Mdesignprocedure.class);
        mdesignprocedureService.limeng_UpDate_Mdesignprocedure(mdesignprocedure);
        if (mdesignprocedure.getDesignmoduletag().equals("G002-2")) {
            Dfile dfile = new Dfile();
            dfile.setProductid(dfID);
            dfile.setDesignmoduletag("W001-1");
            dfileService.DfileUpDate_ZT_LM(dfile);
        }
        return "/li_meng/CPSCGXShengHe.html";
    }

    @RequestMapping("/LiMengUpDateMdesignprocedureCha")
    @ResponseBody
    public PageVo<Mdesignprocedure> LiMengUpDateMdesignprocedureCha(@RequestBody PageVo<Mdesignprocedure> pageVo) {
        return mdesignprocedureService.LiMengUpDateMdesignprocedureCha(pageVo.getNum(), pageVo.getSize());
    }

    @RequestMapping("/LiMengUpDateMdesignprocedure")
    @OpLog(opModule = "生产管理模块",operType = "工序物料设计单变更",operDesc = "此方法用于工序物料设计单变更")
    public String LiMengUpDateMdesignprocedure(String MDesignProcedureDetails, String shu, String mDesignProcedureModuleDetails, String mdesignprocedure) {
        Mdesignprocedure mdesignprocedure1 = new Gson().fromJson(mdesignprocedure, Mdesignprocedure.class);
        Mdesignproceduredetails mapplies = new Gson().fromJson(MDesignProcedureDetails, Mdesignproceduredetails.class);
        List<Integer> ids = new Gson().fromJson(shu, new TypeToken<List<Integer>>() {
        }.getType());
        List<Mdesignproceduremoduledetails> m = new Gson().fromJson(mDesignProcedureModuleDetails, new TypeToken<List<Mdesignproceduremoduledetails>>() {
        }.getType());

        int wuliaoXJ = 0;
        // 产品生产工序物料明细 UpDate
        for (int i = 0; i < m.size(); i++) {
            Mdesignproceduremoduledetails NewM = new Mdesignproceduremoduledetails();
            NewM.setId(m.get(i).getId());
            NewM.setAmount(ids.get(i));
            NewM.setSubtotal(ids.get(i) * m.get(i).getCostprice());
            mdesignproceduremoduledetailsService.limeng_UpDate_Mdesignproceduremoduledetails(NewM);
            wuliaoXJ += m.get(i).getCostprice() * ids.get(i);
        }

        // 产品生产工序明细 update
        Mdesignproceduredetails mdesignproceduredetails = new Mdesignproceduredetails();
        mdesignproceduredetails.setId(mapplies.getId());
        mdesignproceduredetails.setModulesubtotal(wuliaoXJ);
        mdesignproceduredetails.setDesignmodulechangetag("D003-1");
        mdesignproceduredetailsService.updateByMingXi(mdesignproceduredetails);

        // 产品生产工序 update
        List<Mdesignproceduredetails> list = mdesignproceduredetailsService.limeng_UpDateCha_Mdesignproceduredetails(mdesignprocedure1.getId());
        int gongshi = 0;
        int wuliao = 0;
        for (Mdesignproceduredetails lis : list) {
            gongshi += lis.getSubtotal();
            wuliao += lis.getModulesubtotal();
        }
        Mdesignprocedure mdesignprocedure2 = new Mdesignprocedure();
        mdesignprocedure2.setId(mdesignprocedure1.getId());
        mdesignprocedure2.setCostpricesum(gongshi);
        mdesignprocedure2.setModulecostpricesum(wuliao);
        mdesignprocedure2.setDesignmoduletag("G002-1");
        mdesignprocedureService.limeng_UpDate_Mdesignprocedure(mdesignprocedure2);
        return "/li_meng/mdesignprocedureUP(1).html";
    }


}
